/**
 * 基础工具包.
 */
define([ "jquery" ],
    function($) {

      window.isEmpty = function(t) {
        if (t == null || t == undefined) {
          return true;
        }
        return false;
      }

      window.isBlank = function(t) {
        if (!isEmpty(t)) {
          return t.trim && (t.trim() == "");
        }
        return true;
      }

      window.shift = function() {
        var res = [];
        try {
          var arg = arguments[0];
          var len = arg.length;
          if (len && len > 1) {
            for (var i = 1; i < len; i++) {
              res[res.length] = arg[i];
            }
          }
        } catch (e) {
          console.log(e);
        }
        return res;
      };
      
      /**
       * 定义一个方法.
       * @param fnName 方法名.
       * @param fn 方法体.
       * <p>字符串类型</p>
       * @param className 类名.
       * <p>所属类的名字</p>
       */
      window.defineFunction=function(fnName,fn,className){
        if(isEmpty(fnName)){
          return;
        }
        if(isEmpty(className)){
          className=this.class?this.class.name:null;
        }
        if(isEmpty(className)){
          this[fnName]=fn;
        }else{
          this[fnName+"::"+className]=fn;
          this[fnName] = (function(){
            return this[fnName+"::"+className].apply(this,arguments);
          });
        }
      };
      /**
       * .
       */
      window.defFunction=window.defFunc=function(clz,fnName,fn,className){
        window.defineFunction.call(clz.prototype,fnName,fn,className);
      }
      
      
      /**
       * 定义类.
       * <p>支持多继承</p>
       * @param parent 父类.
       * <p>多继承时使用数组</p>
       * @param fnConstructor
       *          构造函数.
       */
      window.defineClass=window.defClass = function(parent,fnConstructor,className) {
        try {
          if(isBlank(className)){
            className="Clz"+uuid().replace(/-/g,'');
          }
          
          //为了使定义的类有正常的类名称，采用eval的方式执行函数的定义语句.
          var tmp = eval("(function "+className+" () {\n"
            +"var self = this;\n"
            +"var _arguments = arguments;\n"
            +"if ($.isArray(parent)) {\n"
            +"  $(parent).each(function() {\n"
            +"    var fn = this;\n"
            +"    fn && $.isFunction(fn) && fn.apply(self, _arguments);\n"// 执行父类构造函数
            +"  });\n"
            +"} else {\n"
            +"  parent && $.isFunction(parent) && parent.apply(self, arguments);\n"// 执行父类构造函数
            +"}\n"
            +"fnConstructor && $.isFunction(fnConstructor)\n"
            +"    && fnConstructor.apply(this, arguments);\n"// 自己构造函数
            +"})\n");
          
          tmp.prototype.settings={};//设置默认settings属性.
          if ($.isArray(parent)) {
            $(parent).each(function() {
              tmp.extends.call(tmp, this);
            })
          } else {
            tmp.extends(parent);
          }
          return tmp;
        } catch (e) {
          console.log(e);
        }
      }
      

      /**
       * 单继承.
       * 
       * @param parentClz
       *          父类.
       * @param ...
       *          父类构造函数参数.
       */
      Function.prototype.extends = function(parentClz) {
        try {
          var clz = this;
          if (parentClz && typeof parentClz == "function") {
            var params = window.shift(arguments);
            var params_txt = "";
            for (x in params) {
              params_txt += "params[" + x + "],"
            }
            if (params_txt.length > 0) {
              params_txt = params_txt.substring(0, params_txt.length - 1);
            }
            var prot = eval("new parentClz(" + params_txt + ")");
            clz.prototype = $.extend(clz.prototype, prot);
            clz.prototype.class = clz;
          }
        } catch (e) {
          console.log(e);
        }
      }

      window.session = window.sessionStorage;
      window.storage = window.localStorage;
      String.prototype.trim = function() {
        return this.replace(/(^\s*)|(\s*$)/g, '');
      };
      /**
       * 正常的uuid.
       * 
       * @returns uuid.
       */
      window.uuid = function() {
        var s = [];
        for (var i = 0; i < 36; i++) {
          s[i] = window.uuid.HEX_DIGITS.substr(
              Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = uuid.HEX_DIGITS.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of
                                                                // the
                                                                // clock_seq_hi_and_reserved
                                                                // to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        return s.join("");
      }
      window.uuid.HEX_DIGITS = "0123456789abcdef";

      /**
       * 不包括分隔符的uuid.
       * 
       * @returns uuidhex字符串.
       */
      window.uuidx = function() {
        var s = [];
        for (var i = 0; i < 32; i++) {
          s[i] = window.uuid.HEX_DIGITS.substr(
              Math.floor(Math.random() * 0x10), 1);
        }
        s[12] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = uuid.HEX_DIGITS.substr((s[16] & 0x3) | 0x8, 1); // bits 6-7 of
                                                                // the
                                                                // clock_seq_hi_and_reserved
                                                                // to 01
        return s.join("");
      }

    });